package controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import models.Assignment;
import models.Course;
import models.CourseRequest;
import models.Instructor;
import models.Submission;
import models.Tutorial;
import others.DumbFS;
import others.SubmissionFile;
import play.mvc.Controller;
import play.mvc.With;

@With (Secure.class)
public class Show extends Controller {
	public static void index() {
		// System.out.println(Application.hash("test"));
		render();
	}

	public static void courses() {
		List<Course> courses = Course.findAll();
		render(courses);
	}

	public static void course(long courseId) {
		Course course = Course.findById(courseId);
		List<Assignment> assignments = Assignment.find("course = ?1 and deadline > ?2 order by deadline asc", course, System.currentTimeMillis()).fetch();
		boolean sIn = false;
		for (Tutorial tut : Security.getStudent().tutorials) {
			if (tut.course == course) {
				sIn = true;
				break;
			}
		}

		CourseRequest requested = CourseRequest.find("byTutorial.courseAndStudent", course, Security.getStudent()).first();
		List<Tutorial> tutorials = Tutorial.find("byCourse.id", courseId).fetch();
		render(course, sIn, assignments, tutorials, requested);
	}

	public static void assignment(long assignmentId) {
		Assignment assignment = Assignment.findById(assignmentId);
		List<Submission> submissions = Submission.find("byAssignmentAndStudent", assignment, Security.getStudent()).fetch();
		render(assignment, submissions);
	}

	public static void submissions() {
		List<Submission> subs = Submission.find("student = ?1", Security.getStudent()).fetch();
		render(subs);
	}

	@Check ("ta")
	public static void assignmentSubmissions(long id) {
		Assignment ass = Assignment.findById(id);
		notFoundIfNull(ass);
		render(ass);
	}

	@Check ("ta")
	public static void submission(long id) throws IOException {
		Submission sub = Submission.findById(id);
		notFoundIfNull(sub);

		ZipFile submissionFile = new ZipFile(DumbFS.getInstance().getSubmission(sub));
		List<SubmissionFile> files = new LinkedList<SubmissionFile>();
		Enumeration entries = submissionFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			if (entry.isDirectory())
				continue;
			StringBuffer buffer = new StringBuffer();
			BufferedReader br = new BufferedReader(new InputStreamReader(submissionFile.getInputStream(entry)));
			while (true) {
				String line = br.readLine();
				if (line == null)
					break;
				buffer.append(line);
				buffer.append("\n");
			}
			String[] stringArray = entry.getName().split("[.]");
			String language = stringArray[stringArray.length-1];
			language = language.equals("txt") ? "Plain" : language.toUpperCase().charAt(0) + language.substring(1).toLowerCase();
			files.add(new SubmissionFile(entry.getName(), buffer.toString(), language));
		}

		render(sub, files);
	}

	@Check ("ta")
	public static void comparisons() {
		List<Course> courses = Course.findAll();
		render(courses);
	}
}
